///////////////////////////////////////////////////////////////////////////////////////
//
//	AvaMods Daily Reboot
//		- Version 1.2
//		- 04.03.2008
//		- diamond-optic
//
//////////////////////////////////////////////////////////////////////////////////
//
// Credit:
//
//	- Watch (vaultdata stuff)
//
//////////////////////////////////////////////////////////////////////////////////
//
// Information:
//
//	- Will reboot your server once a day
//	- Checks to make sure there are no real players
//	- If there is only one player, checks to see if they are spec (afk)
//	- Will only reboot if its between 3AM & 6AM
//
//	- Includes a command to force the server to reboot
//	- Using this command also counts as that days restart
//
//	* WARNING - this will wipe out any temporary bans
//
//////////////////////////////////////////////////////////////////////////////////
//
// Commands:
//
//	avamods_reboot		//Forces the server to reboot
//
//////////////////////////////////////////////////////////////////////////////////
//
// CVARs:
//
//	avamods_dailyreboot "1"	//Turn Plugin ON(1)/OFF(0)
//
//////////////////////////////////////////////////////////////////////////////////
//
// Change Log:
//
//	- 05.28.07 - Version 1.0
//		Initial Release
//
//	- 07.25.07 - Version 1.1
//		It might actually work right now!
//
//	- 09.11.07 - Version 1.2
//		Added command to force a reboot
//		Added CVAR to turn on/off plugin
//		Added 3am hour to check list
//		Increased check delay from 3m to 5m
//
//	- 04.03.08 - Version 1.2 (re-release)
//		Public CVAR
//
///////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////
// Module Include
//
#include <amxmodx>
#include <amxmisc>

new p_plugin

new VERSION[] = "1.2"
new SVERSION[]  = "v1.2 - by diamond-optic (www.AvaMods.com)"

///////////////////////////////////////////////////////////////////////////////////////
// Plugin Init
//
public plugin_init()
{
	register_plugin("AvaMods Daily Reboot",VERSION,"diamond-optic")
	register_cvar("avamods_dailyreboot_stats",SVERSION,FCVAR_SERVER|FCVAR_SPONLY)
	
	p_plugin = register_cvar("avamods_dailyreboot","1")
	
	register_concmd("avamods_reboot", "cmdReboot", ADMIN_IMMUNITY, "- reboots the server")
	
	set_task(300.0,"funcCheck")
}

///////////////////////////////////////////////////////////////////////////////////////
//
// Time/Player check function
//
public funcCheck()
{
	if(!get_pcvar_num(p_plugin))
		return PLUGIN_HANDLED

	new time[3]
	get_time("%H",time,2)
	
	if(equali(time,"03") || equali(time,"3") || equali(time,"04") || equali(time,"4") || equali(time,"05") || equali(time,"5"))
		{
		new today[12], server_reboot[12]
		get_time("%m/%d/%Y",today,11)
	
		if(!vaultdata_exists("server_reboot")) 
			set_vaultdata("server_reboot",today)
	
		get_vaultdata("server_reboot",server_reboot,11)
		
		if(!equal(today,server_reboot)) 
			{
			new players[32], num
			get_players(players,num,"c")
			
			if(num == 0)
				funcReboot(today,server_reboot,num)
			else if(num == 1)
				{
				new lonely_player = players[0]
					
				new team = get_user_team(lonely_player)
				
				if(team != 1 && team != 2)
					{
					server_cmd("kick #%d ^"Daily Server Reboot^"", get_user_userid(lonely_player))	
						
					funcReboot(today,server_reboot,num)
					}
				}				
			}
		}
		
	return PLUGIN_HANDLED
}

///////////////////////////////////////////////////////////////////////////////////////
//
// Reboot Command
//
public cmdReboot(id, level, cid)
{
	if (!cmd_access(id, level, cid, 0))
		return PLUGIN_HANDLED
	
	new today[12],server_reboot[12]
	get_time("%m/%d/%Y",today,11)
	
	if(!vaultdata_exists("server_reboot")) 
		set_vaultdata("server_reboot",today)
		
	get_vaultdata("server_reboot",server_reboot,11)
		
	new players[32], num
	get_players(players,num,"c")
	
	if(num > 0)
		{
		for(new i = 0; i < num; i++)
			{
			new id = players[i]
			
			if(is_user_connected(id))
				server_cmd("kick #%d ^"Server Reboot - Reconnect In A Few Seconds...^"", get_user_userid(id))	
			}
		}
		
	funcReboot(today,server_reboot,num)

	return PLUGIN_HANDLED
}

///////////////////////////////////////////////////////////////////////////////////////
//
// Reboot Function
//
public funcReboot(today[],server_reboot[],players)
{
	set_vaultdata("server_reboot",today)
	
	log_amx("Restarting: (Last Restart: %s) (Today: %s) (Clients: %d)",server_reboot, today, players)
	
	server_cmd("quit") 	
}
